package com.unicair.hancher.disableapp.mvp;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.constraint.ConstraintLayout;
import android.widget.Toast;

import com.unicair.hancher.disableapp.list.MainAdapter;
import com.unicair.hancher.disableapp.list.MainViewHolder;
import com.unicair.hancher.disableapp.utils.AppInfoBean;
import com.unicair.hancher.disableapp.utils.HancherLogUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import static android.Manifest.permission.CHANGE_COMPONENT_ENABLED_STATE;

/**
 * 作者：Hancher
 * 时间：2019/1/12.
 * 邮箱：ytu_shaoweijie@163.com
 * 版本：v1.0
 * <p>
 * 说明：
 */
public class MainPresenter {
    private final PackageManager mPackageManager;
    private MainModel mModel;
    private Context mContext;
    private MainAdapter mAdapter;

    public final static int MENU_APP_ALL = 0;
    public final static int MENU_APP_LAUNCHER = 1;
    public final static int MENU_APP_DISABLE = 2;
    /**
     * 列表应用类型<br>
     * {@link MainPresenter#MENU_APP_ALL},{@link MainPresenter#MENU_APP_LAUNCHER},{@link MainPresenter#MENU_APP_DISABLE}
     */
    private static int type = MENU_APP_LAUNCHER;

    public MainPresenter(Context context,PackageManager packageManager) {
        mPackageManager = packageManager;
        mModel = new MainModel(packageManager);
        mContext = context;
    }

    /**
     * 发起后台读取应用数据请求
     */
    public void updateAllData() {
        HancherLogUtil.d("async query data");
        MainMessage message = new MainMessage(MainMessage.UPDATE_ALL_DATA);
        message.setObject(mPackageManager, type);
        EventBus.getDefault().post(message);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void dispatchMainBusEvent(MainMessage message){
        int type = message.getMessageType();
        Object[] objects = message.getObjects();
        switch (type){
            case MainMessage.UPDATE_ALL_DATA_BACK:
                updateAllDataBack((ArrayList<AppInfoBean>) objects[0]);
                break;
        }
    }

    /**
     * 用数据更新视图
     * @param list
     */
    public void updateAllDataBack(ArrayList<AppInfoBean> list){
        HancherLogUtil.d("list change data get, and then update adapter");
        mAdapter.changeData(list);
        mAdapter.notifyDataSetChanged();
    }

    public void binder() {
        EventBus.getDefault().register(this);
        mModel.binder();
    }

    public void unBinder() {
        EventBus.getDefault().unregister(this);
        mModel.unBinder();
    }

    /**
     * 修改应用是否可用
     * @param appClassName
     * @param enable
     */
    public void changetAppEnable(String appClassName, boolean enable) {
        if (!hasPermission(mContext, "android.permission.CHANGE_COMPONENT_ENABLED_STATE")){
            HancherLogUtil.shortToastDebug(mContext,"have no permission CHANGE_COMPONENT_ENABLED_STATE");
            return;
        }
        if("com.unicair.hancher.disableapp".equals(appClassName)){
            HancherLogUtil.shortToast(mContext,"请不要停用这个应用");
            return;
        }
        HancherLogUtil.shortToastDebug(mContext, "即将修改包名为:\n"+appClassName+"\n可用状态为:"+enable);
        if (enable) {
            mPackageManager.setApplicationEnabledSetting(appClassName,
                    PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0);
        } else {
            mPackageManager.setApplicationEnabledSetting(appClassName,
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER, 0);
        }
        updateAllData();
    }

    /**
     * 判断权限是否拥有
     * @param context
     * @param permission manifest里权限字符串传入
     * @return
     */
    public static boolean hasPermission(Context context, String permission){
        int perm = context.checkCallingOrSelfPermission(permission);
        return perm == PackageManager.PERMISSION_GRANTED;
    }

    public void setAdapter(MainAdapter adapter) {
        mAdapter = adapter;
    }

    /**
     * 修改列表中的应用类型<br>
     * {@link MainPresenter#type},
     * @param newType
     */
    public void showAppType(int newType) {
        type = newType;
        updateAllData();
    }
}
